﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace KioskosJugandoLimpio.Admin
{
    public partial class Graficas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null != Request.QueryString["idEncuestas"])
            {
                Classes.Encuesta encuesta = new Classes.Encuesta(int.Parse(Request.QueryString["idEncuestas"]));
                if (encuesta.idEncuestas == 1)
                {
                    encuesta.agrupador = 4;
                }
                else {
                    encuesta.agrupador = -1;
                }
                DataSet ds = encuesta.respuestas;
                if (Request.QueryString["excel"] == "1")
                {
                    Response.Clear();
                    //Response.Headers.Clear();
                    Response.ContentType = "application/ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment; filename=EncuestaExportada" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<HTML><TABLE>");
                    foreach (DataTable dt in ds.Tables)
                    {
                        if (dt.Rows[0]["DTName"].ToString() == "RawData")
                        {
                            sb.Append("<TR>");
                            foreach (DataColumn dc in dt.Columns)
                            {
                                sb.AppendFormat("<TH>{0}</TH>", dc.ColumnName);
                            }
                            sb.Append("</TR>");
                            foreach (DataRow row in dt.Rows)
                            {
                                sb.Append("<TR>");
                                foreach (DataColumn dc in dt.Columns)
                                {
                                    sb.AppendFormat("<TD>{0}</TD>", row[dc].ToString());
                                }
                                sb.Append("</TR>");
                            }
                        }
                    }
                    sb.Append("</TABLE>");
                    Response.Write(sb.ToString());
                    Response.End();
                }
                excelLink.PostBackUrl = "Graficas.aspx?excel=1&idEncuestas=" + Request.QueryString["idEncuestas"];
                excelLink.CommandName = "excel";
                foreach (DataTable dt in ds.Tables)
                {
                    ClientScript.RegisterArrayDeclaration(dt.Rows[0]["DTName"].ToString(), Classes.JSONHelper.FromDataTable(dt));
                    //Response.Write(Classes.JSONHelper.FromDataTable(dt));
                }
                RegisterStartupScript("flot1", "<script>fnAllPlot();</script>");
                /*System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer( );
                string json =s.Serialize(ds);
                Response.Write(json);*/
                if (Request.QueryString["csv"] == "1")
                {
                    Response.Clear();
                    //Response.Headers.Clear();
                    Response.ContentType = "text/csv";
                    Response.AddHeader("Content-Disposition", "attachment; filename=EncuestaExportada" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv");
                    StringBuilder sb = new StringBuilder();
                    foreach (DataTable dt in ds.Tables)
                    {
                        if (dt.Rows[0]["DTName"].ToString() == "RawData")
                        {
                            foreach (DataColumn dc in dt.Columns)
                            {
                                sb.AppendFormat("\"{0}\",", dc.ColumnName);
                            }
                            sb.Append(Environment.NewLine);
                            foreach (DataRow row in dt.Rows)
                            {
                                foreach (DataColumn dc in dt.Columns)
                                {
                                    sb.AppendFormat("\"{0}\",", row[dc].ToString());
                                }
                                sb.Append(Environment.NewLine);
                            }
                        }
                    }
                    Response.Write(sb.ToString());
                    Response.End();
                }
            }

        }
    }
}